﻿function AccessHistoryController($scope, service, $routeParams, $location, $http, $filter) {

    /// date picker
    $scope.openDayFrom = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.openedDayForm = true;
    };
    $scope.openDayTo = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.openedDayTo= true;
    };
    $scope.initDate = new Date('2016-15-20');
    $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
    $scope.format = $scope.formats[1];
    ////End date picker
    $scope.myPage = 30;
    function getLastWeek(){
        var today = new Date();
        var lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
        return lastWeek ;
    }
    $scope.myUsers = "";
    $scope.dfrom = getLastWeek();
    $scope.dto = new Date();

    //get user name of company for combobox
    $http.get(service.locationUrl + "/companyuserinformation/companyid/" + service.infoUser.Id, {})
        .success(function (data) {
            $scope.rowsUserName = data;
        })
        .error(function (err) {

            console.log("error", err);
        });
    //end
    // get history of the user
    $http.get(service.locationUrl + "/historyofuser/" + service.infoUser.Id, {})
       .success(function (data) {
           $scope.rowsHistory = data;
           paginPage(data, $scope.myPage);
       })
       .error(function (err) {

           console.log("error", err);
       });
    // end
    function paginPage(data, pageNum)
    {
        // init
        var sortingOrder = 'Name';
        $scope.sortingOrder = sortingOrder;
        $scope.reverse = false;
        $scope.filteredItems = [];
        $scope.groupedItems = [];
        $scope.itemsPerPage = pageNum;
        $scope.pagedItems = [];
        $scope.currentPage = 0;
        $scope.items = data;
        console.log("data item", $scope.items);
        var searchMatch = function (haystack, needle) {
            if (!needle) {
                return true;
            }
            return haystack.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
        };

        // init the filtered items
        $scope.search = function () {
            $scope.filteredItems = $filter('filter')($scope.items, function (item) {
                for (var attr in item) {
                    if (searchMatch(item[attr], $scope.query))
                        return true;
                }
                return false;
            });
            // take care of the sorting order
            if ($scope.sortingOrder !== '') {
                $scope.filteredItems = $filter('orderBy')($scope.filteredItems, $scope.sortingOrder, $scope.reverse);
            }
            $scope.currentPage = 0;
            // now group by pages
            $scope.groupToPages();
        };

        // calculate page in place
        $scope.groupToPages = function () {
            $scope.pagedItems = [];

            for (var i = 0; i < $scope.filteredItems.length; i++) {
                if (i % $scope.itemsPerPage === 0) {
                    $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)] = [$scope.filteredItems[i]];
                } else {
                    $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)].push($scope.filteredItems[i]);
                }
            }
        };

        $scope.range = function (start, end) {
            var ret = [];
            if (!end) {
                end = start;
                start = 0;
            }
            for (var i = start; i < end; i++) {
                ret.push(i);
            }
            return ret;
        };

        $scope.prevPage = function () {
            if ($scope.currentPage > 0) {
                $scope.currentPage--;
            }
        };

        $scope.nextPage = function () {
            if ($scope.currentPage < $scope.pagedItems.length - 1) {
                $scope.currentPage++;
            }
        };

        $scope.setPage = function () {
            $scope.currentPage = this.n;
        };

        // functions have been describe process the data for display
        $scope.search();

        // change sorting order
        $scope.sort_by = function (newSortingOrder) {
            if ($scope.sortingOrder == newSortingOrder)
                $scope.reverse = !$scope.reverse;

            $scope.sortingOrder = newSortingOrder;

            // icon setup
            $('th i').each(function () {
                // icon reset
                $(this).removeClass().addClass('icon-sort');
            });
            if ($scope.reverse)
                $('th.' + new_sorting_order + ' i').removeClass().addClass('icon-chevron-up');
            else
                $('th.' + new_sorting_order + ' i').removeClass().addClass('icon-chevron-down');
        };
    }

    $scope.update = function ()
    {
        console.log($scope.myPage);
        // get history of the user
        $http.get(service.locationUrl + "/historyofuser/" + service.infoUser.Id, {})
           .success(function (data) {
               $scope.rowsHistory = data;
               paginPage(data, $scope.myPage);
           })
           .error(function (err) {

               console.log("error", err);
           });
        // end
    }
}

